package com.itheima.stock.util;

import com.itheima.stock.entity.StockRtInfo;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author CaiZhiMin
 * @created 2025/10/21 10:15
 * @description: 股票各值计算工具类
 */
public class StockCalcUtils {
    // 振幅 = (最高价 - 最低价) / 昨收 * 100
    public static BigDecimal calcAmplitude(StockRtInfo info) {
        if (info.getPreClosePrice() == null || info.getPreClosePrice().compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        return info.getMaxPrice()
                .subtract(info.getMinPrice())
                .divide(info.getPreClosePrice(), 6, RoundingMode.HALF_UP)
                .multiply(BigDecimal.valueOf(100));
    }

    // 涨跌 = 当前价 - 昨收
    public static BigDecimal calcUpDown(StockRtInfo info) {
        if (info.getCurPrice() == null || info.getPreClosePrice() == null) {
            return BigDecimal.ZERO;
        }
        return info.getCurPrice().subtract(info.getPreClosePrice());
    }

    // 涨幅 = (当前价 - 昨收) / 昨收 * 100
    public static BigDecimal calcIncrease(StockRtInfo info) {
        if (info.getPreClosePrice() == null || info.getPreClosePrice().compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        return info.getCurPrice()
                .subtract(info.getPreClosePrice())
                .divide(info.getPreClosePrice(), 6, RoundingMode.HALF_UP)
                .multiply(BigDecimal.valueOf(100));
    }
}
